/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.app.valid.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.app.valid.entity.TvalidCode;
import com.jeesite.modules.app.valid.service.TvalidCodeService;
import com.jeesite.modules.conf.AppController;
import com.jeesite.modules.conf.MessageCode;
import com.jeesite.modules.msg.entity.content.SmsMsgContent;
import com.jeesite.modules.msg.utils.MsgPushUtils;

/**
 * 验证码记录表Controller
 * @author zpp
 * @version 2020-01-24
 */
@Controller
@RequestMapping(value = "/app/valid/")
public class TvalidCodeController {

	@Autowired
	private TvalidCodeService tvalidCodeService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public TvalidCode get(String id, boolean isNewRecord) {
		return tvalidCodeService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("app:valid:tvalidCode:view")
	@RequestMapping(value = {"list", ""})
	public String list(TvalidCode tvalidCode, Model model) {
		model.addAttribute("tvalidCode", tvalidCode);
		return "modules/app/valid/tvalidCodeList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("app:valid:tvalidCode:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<TvalidCode> listData(TvalidCode tvalidCode, HttpServletRequest request, HttpServletResponse response) {
		tvalidCode.setPage(new Page<>(request, response));
		Page<TvalidCode> page = tvalidCodeService.findPage(tvalidCode);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("app:valid:tvalidCode:view")
	@RequestMapping(value = "form")
	public String form(TvalidCode tvalidCode, Model model) {
		model.addAttribute("tvalidCode", tvalidCode);
		return "modules/app/valid/tvalidCodeForm";
	}

	/**
	 * 保存验证码记录表
	 */
	@RequiresPermissions("app:valid:tvalidCode:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated TvalidCode tvalidCode) {
		tvalidCodeService.save(tvalidCode);
		return "保存验证码记录表成功！";
	}
	
	/**
	 * 停用验证码记录表
	 */
	@RequiresPermissions("app:valid:tvalidCode:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(TvalidCode tvalidCode) {
		tvalidCode.setStatus(TvalidCode.STATUS_DISABLE);
		tvalidCodeService.updateStatus(tvalidCode);
		return "停用验证码记录表成功";
	}
	
	/**
	 * 启用验证码记录表
	 */
	@RequiresPermissions("app:valid:tvalidCode:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(TvalidCode tvalidCode) {
		tvalidCode.setStatus(TvalidCode.STATUS_NORMAL);
		tvalidCodeService.updateStatus(tvalidCode);
		return "启用验证码记录表成功";
	}
	
	/**
	 * 删除验证码记录表
	 */
	@RequiresPermissions("app:valid:tvalidCode:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(TvalidCode tvalidCode) {
		tvalidCodeService.delete(tvalidCode);
		return "删除验证码记录表成功！";
	}
	@RequestMapping(value="validcode")
	public void validcode(HttpServletRequest request,HttpServletResponse response){
		String mobile=request.getParameter("mobile");
		if(StringUtils.isEmpty(mobile)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "手机号");
			return;
		}
		String fpValidCode = com.jeesite.common.lang.StringUtils.getRandomNum(6);
		TvalidCode tvalidCode=new TvalidCode();
		tvalidCode.setValidCode(Integer.parseInt(fpValidCode));
		tvalidCode.setMobile(mobile);
		tvalidCodeService.save(tvalidCode);
		String str=JsonMapper.toJson(tvalidCode);
		JSONObject json = JSONObject.parseObject(str);
		AppController.renderSuccessObject(response, json, MessageCode.SUCCESS_ACK);
	}
	private static void sendSMS(String fpValidCode){
		SmsMsgContent msgContent=new SmsMsgContent();
		msgContent.setTitle("提示信息");
		msgContent.setContent("您好，您的验证码是："+fpValidCode+"(请勿透露给其他人)感谢您的使用。");
		MsgPushUtils.push(msgContent, "BizKey", "BizType", "15510103294");
	}
	public static void main(String[] args) {
		String fpValidCode = com.jeesite.common.lang.StringUtils.getRandomNum(6);
		sendSMS(fpValidCode);
		
	}
	
}